package com.siysoft.tc.teacher.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siysoft.tc.base.bean.bo.MRDetailBo;
import com.siysoft.tc.base.bean.bo.PTDetailBo;
import com.siysoft.tc.base.bean.bo.PaginationBo;
import com.siysoft.tc.base.bean.bo.TeacherBo;
import com.siysoft.tc.base.bean.bo.TeacherDetailBo;
import com.siysoft.tc.teacher.service.ITeacherService;
import com.siysoft.util.COMMON;
import com.tc.base.controller.BaseController;

@Controller
@RequestMapping(value = "/app/teacher")
public class TeacherController extends BaseController{
	@Autowired
	ITeacherService teacherService;
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/loading")
	public void loading(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					|| COMMON.isnull(request.getParameter("school"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			String school = request.getParameter("school").toString();
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<TeacherBo> teacher = teacherService.getAll(pb, school);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("teacher", COMMON.NulltoBlank(teacher));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/search")
	public void search(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("school"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			String lecture = request.getParameter("lecture").toString();
			String school = request.getParameter("school").toString();
			String course = request.getParameter("course").toString();
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<TeacherBo> teacher = teacherService.getTeacher(lecture, course, school, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("teacher", COMMON.NulltoBlank(teacher));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/search2")
	public void search2(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("select_str"))
					|| COMMON.isnull(request.getParameter("school"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			String select_str = request.getParameter("select_str").toString();
			String school = request.getParameter("school").toString();
			
			List<TeacherBo> teacher = teacherService.search2teacher(select_str, school);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("teacher", COMMON.NulltoBlank(teacher));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}

	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/detail")
	public void detail(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("reservation_limit"))
					|| COMMON.isnull(request.getParameter("reservation_offset"))
					|| COMMON.isnull(request.getParameter("assessment_limit"))
					|| COMMON.isnull(request.getParameter("assessment_offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer reservation_limit = Integer.valueOf(request.getParameter("reservation_limit").toString());
			Integer reservation_offset = Integer.valueOf(request.getParameter("reservation_offset").toString());
			Integer assessment_limit = Integer.valueOf(request.getParameter("assessment_limit").toString());
			Integer assessment_offset = Integer.valueOf(request.getParameter("assessment_offset").toString());
			
			PaginationBo reservation_pb = new PaginationBo(reservation_limit, reservation_offset, 0);
			PaginationBo assessment_pb = new PaginationBo(assessment_limit, assessment_offset, 0);
			TeacherDetailBo teacher_detail = teacherService.getDetail(id, reservation_pb, assessment_pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("teacher_detail", COMMON.NulltoBlank(teacher_detail));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/pt")
	public void pt(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			
			PTDetailBo pt_detail = teacherService.getPTDetail(id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("pt_detail", COMMON.NulltoBlank(pt_detail));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/mr")
	public void mr(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			
			MRDetailBo mr_detail = teacherService.getMRDetail(id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("mr_detail", COMMON.NulltoBlank(mr_detail));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/complain")
	public void complain(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("number"))
					|| COMMON.isnull(request.getParameter("type"))
					|| COMMON.isnull(request.getParameter("detail"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			String number = request.getParameter("number").toString();
			String type = request.getParameter("type").toString();
			String detail = request.getParameter("detail").toString();
			
			String result = teacherService.complain(id, number, type, detail);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}

}
